/*************************************************************************
	> File Name: test14.cc
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/09/21 13:38
 ************************************************************************/

#include <iostream>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

void handler(int sig)
{
    pid_t id;
    while ((id == waitpid(-1, nullptr, WNOHANG)) > 0) {
        printf("Wait child success:%d\n", id);
    }
    printf("Child is quit!%d\n", getpid());
}

int main(int argc, char *argv[])
{
    signal(SIGCHLD, handler);
    pid_t cid;
    if ((cid == fork()) == 0) {
        printf("Child:%d\n", getpid());
        sleep(3);
        exit(1);
    }
    while (1) {
        printf("Father process is executing other tasks.\n");
        sleep(1);
    }
    return 0;
}
